Page({
  data: {
    username: '',
    password: '',
    showSlider: false,
    sliderWidth: 0, // 百分比单位
    sliderLeft: 0, // px单位
    startX: 0,
    isDragging: false,
    isVerified: false,
    containerWidth: 0, // 滑块容器宽度(px)
    blockWidth: 60,    // 滑块宽度(px)
    isLoading: false   // 加载状态
  },

  onLoad() {
    setTimeout(() => {
      this.getSliderContainerWidth();
    }, 300);
  },

  // 获取滑块容器宽度
  getSliderContainerWidth() {
    const query = wx.createSelectorQuery();
    query.select('.slider-outer').boundingClientRect(rect => {
      if (rect) {
        this.setData({
          containerWidth: rect.width
        });
      } else {
        setTimeout(() => this.getSliderContainerWidth(), 200);
      }
    }).exec();
  },

  // 用户名输入事件
  onUsernameInput(e) {
    this.setData({ username: e.detail.value });
    this.checkAndShowSlider();
  },

  // 密码输入事件
  onPasswordInput(e) {
    this.setData({ password: e.detail.value });
    this.checkAndShowSlider();
  },

  // 检查并显示滑块
  checkAndShowSlider() {
    const { username, password } = this.data;
    const shouldShow = !!username.trim() && !!password.trim();
    this.setData({ showSlider: shouldShow });
    if (shouldShow) {
      setTimeout(() => this.getSliderContainerWidth(), 100);
    }
  },

  // 触摸开始
  onSliderStart(e) {
    if (!this.data.containerWidth) return;
    this.setData({
      startX: e.touches[0].clientX,
      isDragging: true
    });
  },

  // 触摸移动
  onSliderMove(e) {
    if (!this.data.isDragging || !this.data.containerWidth) return;

    const { startX, containerWidth, blockWidth } = this.data;
    const currentX = e.touches[0].clientX;
    const moveX = currentX - startX;

    const maxLeft = containerWidth - blockWidth;
    let left = Math.max(0, Math.min(moveX, maxLeft));

    this.setData({
      sliderLeft: left,
      sliderWidth: (left / maxLeft) * 100
    });
  },

  // 触摸结束
  onSliderEnd() {
    if (!this.data.containerWidth) return;

    this.setData({ isDragging: false });
    const { containerWidth, blockWidth, sliderLeft } = this.data;
    const maxLeft = containerWidth - blockWidth;

    if (sliderLeft >= maxLeft * 0.95) {
      this.setData({
        sliderLeft: maxLeft,
        sliderWidth: 100,
        isVerified: true
      });
    } else {
      this.setData({
        sliderLeft: 0,
        sliderWidth: 0,
        isVerified: false
      });
    }
  },

  // 触摸被中断
  onSliderCancel() {
    this.setData({ isDragging: false });
  },
// 跳转到注册页面
navigateToRegister() {
  wx.navigateTo({
    url: '/pages/zhuc/zhuc' 
  })
},
  login() {
    const { username, password } = this.data;

    this.setData({ isLoading: true });
    
    wx.request({
      url: 'http://localhost:8080/nurse/login',
      method: 'POST',
      data: {
        username: username.trim(),
        password: password.trim()
      },
      header: { 
        'content-type': 'application/x-www-form-urlencoded' 
      },
      success: (res) => {
        this.setData({ isLoading: false });
        
        if (res.data.code === 200) {
          wx.showToast({ title: '登录成功', icon: 'success' });
          wx.setStorageSync('userInfo', res.data.data);
          setTimeout(() => wx.switchTab({ url: '/pages/zhuye/zhuye' }), 1000);
        } else {
          wx.showToast({ title: res.data.msg || '登录失败', icon: 'none' });
          this.resetSlider();
        }
      },
      fail: (err) => {
        this.setData({ isLoading: false });
        wx.showToast({ title: '接口连接失败', icon: 'none' });
        console.error('登录失败：', err);
        this.resetSlider();
      }
    });
  },

  // 重置滑块
  resetSlider() {
    this.setData({
      sliderLeft: 0,
      sliderWidth: 0,
      isVerified: false
    });
  }
});